package com.gkr.shopping.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gkr.shopping.bean.Article;
import com.gkr.shopping.bean.ArticleType;
import com.gkr.shopping.pager.PageModel;
import com.gkr.shopping.service.ArticleService;


@Controller
@RequestMapping("/article")
public class ArticleController {
	
	@Autowired
	private ArticleService articleService;
		
	
	@RequestMapping("/index")
	public String  articleIndex(Model model,String typeCode,String keyword,PageModel pageModel) {
	
		List<ArticleType>  articleTypes = articleService.findAllFirstArticleType();
		model.addAttribute("articleTypes", articleTypes);
		System.out.println("typeCode:"+typeCode);
		if(typeCode != null && !typeCode.equals("")) {
			String code = typeCode.substring(0, 4);
			List<ArticleType> seArticleTypes = articleService.findAllSecondArticleTypes(code+"%");
			model.addAttribute("secondArticleTypes", seArticleTypes);
		}

		List<Article> articles = articleService.findAllArticle(typeCode == null ? null : typeCode+"%",keyword == null ? null : "%"+keyword+"%",pageModel);
		model.addAttribute("articles", articles);
		
		int totalNum = articleService.findTotalNum(typeCode == null ? null : typeCode+"%",keyword == null ? null : "%"+keyword+"%");
		pageModel.setTotalNum(totalNum);
		
	   return "articleIndex";
	}
	
	@RequestMapping("/detail")
	public String articleDetail(Integer id,Model model) {
		Article article = articleService.getArticleById(id);
		model.addAttribute("article", article);
		return "articleDetail";
	}

}
